package com.pvj.myaccount.ui.summarize

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.pvj.myaccount.repository.HomeRepository
import com.pvj.myaccount.room.entity.Cost
import com.pvj.myaccount.uitls.TimeUtil
import kotlinx.coroutines.launch

class SummarizeViewModel : ViewModel() {
    private val repository = HomeRepository()

    private val _costList = MutableLiveData<List<Cost>?>()
    val yearList: LiveData<List<Cost>?> = _costList

    fun loadYearDataAsync(year: Int) {
        val todayStart = TimeUtil.getFirstDayOfMonthTimestamp(year,1)
        val todayEnd = TimeUtil.getLastDayOfMonthTimestamp(year,12)
        Log.d("SummarizeViewModel", "loadMonthData year:${year} ;start:${todayStart} end:${todayEnd}")

        viewModelScope.launch {
            val data = repository.getData(todayStart, todayEnd)
            // 更新LiveData
            _costList.postValue(data)
        }
    }


}